﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// UserConfirms 类。
	/// </summary>
	internal static partial class UserConfirms
	{
		#region GetItem
		/// <summary>
		/// 根据标识获取指定的用户验证。
		/// </summary>
		/// <param name="o">用户验证集合。</param>
		/// <param name="userId">获取的用户验证标识 UserId。</param>
		/// <returns>获取的信息项。</returns>
		public static UserConfirm GetItem(this System.Data.Linq.Table<UserConfirm> o, Guid userId)
		{
			return o.SingleOrDefault(p => p.UserId == userId);
		}

		/// <summary>
		/// 根据标识获取指定的用户验证。
		/// </summary>
		/// <param name="userId">获取的用户验证标识 UserId。</param>
		/// <returns>获取的信息项。</returns>
		public static UserConfirm GetItem(Guid userId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.UserConfirms.GetItem(userId);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的用户验证是否已经存在。
		/// </summary>
		/// <param name="o">用户验证集合。</param>
		/// <param name="userId">验证的用户验证标识 UserId。</param>
		/// <returns>布尔值表示指定的用户验证是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<UserConfirm> o, Guid userId)
		{
			return o.Any(p => p.UserId == userId);
		}

		/// <summary>
		/// 验证指定的用户验证是否已经存在。
		/// </summary>
		/// <param name="userId">验证的用户验证标识 UserId。</param>
		/// <returns>布尔值表示指定的用户验证是否已经存在。</returns>
		public static bool Exists(Guid userId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.UserConfirms.Exists(userId);
		}
		#endregion
	}




	/// <summary>
	/// UserConfirms 类操作部分。
	/// </summary>
	internal static partial class UserConfirms
	{
		#region Confirm
		/// <summary>
		/// 用户确认。
		/// </summary>
		/// <param name="userId">进行确认的用户标识。</param>
		/// <param name="confirmCode">确认码。</param>
		public static bool Confirm(Guid userId, string confirmCode)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			UserConfirm confirm = dc.UserConfirms.GetItem(userId);
			if (confirm == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			if (confirm.ConfirmCode != confirmCode) return false;

			confirm.IsConfirm = true;

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			return true;
		}
		#endregion




		#region Update
		private static readonly HashSet<string> _updatedExcludeColumns = typeof(UserConfirm).GetEntityColumnNames(new string[] {
				"UserId"
			}, true);

		/// <summary>
		/// 修改用户验证。
		/// </summary>
		/// <param name="updatedConfirm">修改的用户验证。</param>
		/// <returns>修改的用户验证。</returns>
		internal static UserConfirm Update(UserConfirm updatedConfirm)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			UserConfirm confirm = dc.UserConfirms.GetItem(updatedConfirm.UserId);
			if (confirm == null)
			{
				confirm = updatedConfirm.ToNewEntity<UserConfirm>();
				dc.UserConfirms.InsertOnSubmit(confirm);
			}
			else
				confirm.FillByEntity(updatedConfirm, EntityColumnsSetType.Exclude, _updatedExcludeColumns);

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 返回只读数据
			return updatedConfirm.FillByEntity(confirm);
		}
		#endregion




		#region Delete
		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, UserConfirm confirm)
		{
			dc.UserConfirms.DeleteOnSubmit(confirm);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, User parent)
		{
			if (parent.UserConfirm != null)
				Delete(dc, parent.UserConfirm);
		}
		#endregion
		#endregion
	}
}